$(document).ready(function () {
    var murl = window.location.href;
    var prefix = murl.split("/index")[0];
    //追加展示的商品块方法
    function appendToGoodList(item) {
        $("#goodList").append('<li id = "goodItem" data-goodID="'+item.id+'" data-publisherID="'+item.publisherid +
            '" data-content="'+item.content +
            '"><a title="'+item.title+'" href="'+prefix+'/item.html?gid='+item.id+'&pid='+item.publisherid+'">' +
            '<img width="100" height="100" src="'+item.picture+'"></a>' +
            '<div><a title="'+item.title+'" href="#">'+item.title+'</a></div>' +
            '<div><strong>'+item.price+'</strong></div></li>');
    }
    //验证登录
    $.ajax({
        type: "GET",
        url: "load",
        dataType: "json",
        data: {},
        timeout: 10000,
        async:false,
        cache:false,
        success: function (res) {
            if(res.result != "yes") {
                window.location.href = "login";
            }
        }
    });
    //默认加载最新发布
    $("#goodList").empty();
    var exed = false;
    if(!exed)
        $.ajax({
        type: "GET",
        url: "index/good",
        dataType: "json",
        data: {
            "fromTime":new Date().valueOf(),
            "startat":0,
            "endat":11
        },
        timeout: 10000,
        async:false,
        success: function (res) {
            if (res.result == 'ERROR') {
                alert(res.reason);
            }
            if (res.result == 'FAILED') {
                alert(res.reason);
            }
            if(res.result == 'SUCCESS'){
                exed = true;
                var goods = res.data;
                for(var i=0;i<goods.length;i++){
                    var item = goods[i];
                    appendToGoodList(item);
                }
            }
        }
    });
    //选择分类查看最新发布
    $("#typeList").find("a").click(function () {
        var type = $(this).text();
        $.ajax({
            type: "GET",
            url: "index/good",
            dataType: "json",
            data: {
                "fromTime":new Date().valueOf(),
                "startat":0,
                "endat":12,
                "tag":encodeURI(type)
            },
            timeout: 10000,
            async:true,
            success: function (res) {
                if (res.result == 'ERROR') {
                    alert(res.reason);
                }
                if (res.result == 'FAILED') {
                    alert(res.reason);
                }
                if(res.result == 'SUCCESS'){
                    exed = true;
                    var goods = res.data;
                    $("#goodList").empty();
                    for(var i=0;i<goods.length;i++){
                        var item = goods[i];
                        appendToGoodList(item);
                    }
                }
            }
        });
    });
    //注销
    $("#logout").click(function () {
        $.ajax({
            type: "POST",
            url: "logout",
            dataType: "json",
            data: {},
            timeout: 10000,
            async:false,
            success: function (res) {
                if (res.result != 'SUCCESS') {
                    alert(res.reason);
                }else
                    window.location.href = "login.html";


            }
        });
    });
    //搜索功能
    $("#searchButton").unbind('click');
    $("#searchButton").click(function () {
        var keyword = $("#keywordText").val();
        if(keyword == null || keyword == '')
            return;
        $.ajax({
            type: "GET",
            url: "index/good/search",
            dataType: "json",
            data: {
                "fromTime":new Date().valueOf(),
                "startat":0,
                "endat":12,
                "keyword":encodeURI(keyword)
            },
            timeout: 10000,
            async:false,
            scriptCharset: 'utf-8',
            success: function (res) {
                if (res.result == 'ERROR') {
                    alert(res.reason);
                }
                if (res.result == 'FAILED') {
                    alert(res.reason);
                }
                if(res.result == 'SUCCESS'){
                    $("#goodList").empty();
                    $("#tagList").remove();
                    $("#subtitle").text("搜索结果");
                    var goodsearch = res.data;
                    for(var i=0;i<goodsearch.length;i++){
                        var item = goodsearch[i];
                        appendToGoodList(item);
                    }
                }
            }
        });
    });
    //发布按钮
    $("#publishButton").click(function () {
       window.location.href = prefix+"/publish.html";
    });
});